package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 天易网
 * @author kd-15
 *
 */
public class WolfaxNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.wolfax.com/portal.php[?]aid=[0-9]*&mod=view",
		"http://[a-z]*.wolfax.com/home.php[?]do=blog&id=[0-9]*&mod=space&uid=[0-9]*",
		"http://www.wolfax.com/forum.php[?]mod=viewthread&tid=[0-9]*",
		"http://www.wolfax.com/a-[0-9]*-1.html",
		"http://bbs.wolfax.com/t-[0-9]*-1-1.html",
		"http://bbs.wolfax.com/forum.php[?]mod=viewthread&tid=[0-9]*"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1.ph").text();
		if(title.contains("-")) title=StringUtils.substringBefore(title, "-");
		if(StringUtils.isBlank(title)){
			title =doc.select("span#thread_subject").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td#article_content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#blog_article"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.t_f"));
		}
		date =DateUtils.matchDate(doc.select("p.xg1").text());
		if(date==null){
			date =DateUtils.matchDate(doc.select("span.xg1").text());
		}
		if(date==null){
			date =DateUtils.matchDate(doc.select("em:contains(发表于)").text());
		}
		author=JSoupUtils.matchAuthor(doc, "发布者:");
		if(StringUtils.isBlank(author)){
			author=doc.select("a.xw1").text();
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
